package com.ourway;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.widget.TextView;

import com.facebook.Request.GraphUserCallback;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;

public class MainActivity extends Activity{
	
	public Session ses;
	private User profUser = new User();
	TextView tv;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//tv = (TextView) findViewById(R.id.tvUser);
		
		ses = Session.openActiveSession(this, true, statusCallback);
		
		
		ActionBar actionBar = getActionBar();
	    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
	    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
	    
	    Tab tab = actionBar
	            .newTab()
	            .setIcon(R.drawable.ic_location_map)
	            .setTabListener(new MyTabListener<MapFragment>(this, "artist", MapFragment.class));
	        actionBar.addTab(tab);

	        tab = actionBar
	            .newTab()
	            .setIcon(R.drawable.ic_social_group)
	            .setTabListener(new MyTabListener<FriendsFragment>(this, "album", FriendsFragment.class));
	        actionBar.addTab(tab);
	        

	        tab = actionBar
		            .newTab()
		            .setIcon(R.drawable.ic_social_person)
		            .setTabListener(new MyTabListener<FriendsFragment>(this, "album", FriendsFragment.class));
		    actionBar.addTab(tab);
		    
	        tab = actionBar
		            .newTab()
		            .setIcon(R.drawable.ic_social_group)
		            .setTabListener(new MyTabListener<FriendsFragment>(this, "album", FriendsFragment.class));
		    actionBar.addTab(tab);
		        
		    tab = actionBar
			            .newTab()
			            .setIcon(R.drawable.ic_action_search)
			            .setTabListener(new MyTabListener<FriendsFragment>(this, "album", FriendsFragment.class));
		    actionBar.addTab(tab);
	    //View view = View.inflate(getApplicationContext(), R.layout.actionbar_main, null);
	    //View view = LayoutInflater.from(this).inflate(R.layout.actionbar_main, null);
	    //actionBar.setCustomView(view);
	}
	

	  public static class MyTabListener<T extends Fragment> implements
	      TabListener {
	    private Fragment mFragment;
	    private final Activity mActivity;
	    private final String mTag;
	    private final Class<T> mClass;

	    
	    public MyTabListener(Activity activity, String tag, Class<T> clz) {
	      mActivity = activity;
	      mTag = tag;
	      mClass = clz;
	    }

	    /* The following are each of the ActionBar.TabListener callbacks */

	    public void onTabSelected(Tab tab, FragmentTransaction ft) {
	      // Check if the fragment is already initialized
	    	if (mFragment == null) {
	    		// If not, instantiate and add it to the activity
	    		mFragment = Fragment.instantiate(mActivity, mClass.getName());
	    		ft.add(android.R.id.content, mFragment, mTag);
	    		} 
	    	else {
	    		// If it exists, simply attach it in order to show it
	    		ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
	    		ft.attach(mFragment);
	    		}
	    }

	    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
	    	if (mFragment != null) {
	    		ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
	    		ft.detach(mFragment);
	    	}
	    }

	    public void onTabReselected(Tab tab, FragmentTransaction ft) {
	    }
	  }
	  
//	@Override
//	public boolean onCreateOptionsMenu(Menu menu){
//		getMenuInflater().inflate(R.menu.activity_main, menu);
//		return super.onCreateOptionsMenu(menu);
//	}
	


	
	private Session.StatusCallback statusCallback = new Session.StatusCallback() {
		
		@Override
		public void call(Session session, SessionState state, Exception exception) {
			// TODO Auto-generated method stub
			if(session.isOpened()){
				com.facebook.Request.executeMeRequestAsync(session, new GraphUserCallback() {
					
					@Override
					public void onCompleted(GraphUser user, Response response) {
						// TODO Auto-generated method stub
						if(user != null){
							profUser.setGraphUser(user.getName());
							//tv = (TextView) findViewById(R.id.tvUser);
							//tv.setText(profUser.getFullName());
						}
					}
				});			
			}
		}
	};

}
